import 'package:flutter/material.dart';
import 'package:qunaimedical/components/edit_text_field.dart';
import 'package:qunaimedical/components/primary_button.dart';
import 'package:qunaimedical/components/primary_label.dart';
import 'package:qunaimedical/models/map_response.dart';
import 'package:qunaimedical/network/http.dart';
import 'package:qunaimedical/utils/common_util.dart';
import 'package:qunaimedical/utils/dialog_utils.dart';
import 'package:qunaimedical/utils/sp_utils.dart';
import 'package:qunaimedical/utils/toast_util.dart';
import 'package:qunaimedical/values/urls.dart';

/// 意见反馈
class FeedbackPage extends StatefulWidget {
  const FeedbackPage({Key key}) : super(key: key);

  @override
  _FeedbackPageState createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  TextEditingController _controller = TextEditingController();

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('意见反馈'),
      ),
      body: ListView(
        children: [
          PrimaryLabel('意见反馈'),
          EditTextField(
            maxLines: 5,
            maxLength: 400,
            hintText: '请描述您所遇到的困难',
            controller: _controller,
          ),
          PrimaryButton('提交', onPressed: _post),
        ],
      ),
    );
  }

  void _post() async {
    CommonUtils.hideKeyboard(context);
    String content = _controller.text.trim();
    if (content.isEmpty) {
      ToastUtil.show('内容不能为空', context);
      return;
    }
    DialogUtils.showWaitDialog(context);
    var data = await XHttp.postJson(Urls.FEEDBACK, {
      "feedbackContent": content, // 反馈内容
      "feedbackDate": '${DateTime.now()}'.substring(0, 19), // 反馈时间
      "feedbackAccountId": SPUtils.getUser().id, // 反馈账号
      "feedbackAccountType": "1" // 反馈账号类型（1:医生;0:患者）
    });
    Navigator.pop(context);
    if (data != null) {
      var response = MapResponse.fromJson(data);
      if (response.code == 1) {
        DialogUtils.showAlertDialog(context, '反馈成功', '感谢您宝贵的意见，我们会继续努力',
            onPressed: () {
          Navigator.pop(context);
          Navigator.pop(context);
        });
      } else {
        ToastUtil.show(response.msg, context);
      }
    }
  }
}
